package com.tencent.qcloud.tuikit.tuichat.zhiyu.viewholder;

import android.graphics.Color;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.tencent.qcloud.tuicore.TUIThemeManager;
import com.tencent.qcloud.tuikit.timcommon.bean.TUIMessageBean;
import com.tencent.qcloud.tuikit.timcommon.classicui.widget.message.MessageContentHolder;
import com.tencent.qcloud.tuikit.tuichat.R;
import com.tencent.qcloud.tuikit.tuichat.zhiyu.bean.CustomZhiyuMessageBean;
import com.tencent.qcloud.tuikit.tuichat.zhiyu.bean.SendGifts1v1Bean;
import com.ysg.utils.YImageUtil;
import com.ysg.utils.YStringUtil;

import org.json.JSONException;
import org.json.JSONObject;

public class CustomZhiyuMessageHolder extends MessageContentHolder {
    private TextView msgBodyText;
    private ImageView iv_imageGift;
    private TextView tv_Shuoming;
    private LinearLayout ll_sendgifts;
    private TextView tv_giftsName;
    private LinearLayout ll_ItemLayout;
    private TextView tvServiceContent;
    private LinearLayout llServiceTip;

    public CustomZhiyuMessageHolder(View rootView) {
        super(rootView);
        msgBodyText = rootView.findViewById(R.id.msg_body_tv);
        iv_imageGift = rootView.findViewById(R.id.iv_imageGift);
        tv_Shuoming = rootView.findViewById(R.id.tv_Shuoming);
        ll_sendgifts = rootView.findViewById(R.id.ll_sendgifts);
        tv_giftsName = rootView.findViewById(R.id.tv_giftsName);
        ll_ItemLayout = rootView.findViewById(R.id.ll_ItemLayout);
//        llContent = rootView.findViewById(R.id.llContent);
        llServiceTip = rootView.findViewById(R.id.llServiceTip);
        tvServiceContent = rootView.findViewById(R.id.tvServiceContent);
    }

    public static final String TAG = CustomZhiyuMessageHolder.class.getSimpleName();

    @Override
    public int getVariableLayout() {
        return R.layout.message_adapter_content_zhiyu;
    }

    @Override
    public void layoutVariableViews(TUIMessageBean msg, int position) {
        msgBodyText.setVisibility(View.VISIBLE);
        String data = new String(msg.getV2TIMMessage().getCustomElem().getData());
        Log.i("===自定义消息体内容==", "" + data);
        msg.setExtra(data);
        if (msg.getExtra() != null) {
            if (YStringUtil.contains(msg.getExtra(), "@@@@@***")){
                //礼物
                llServiceTip.setVisibility(View.GONE);
                msgBodyText.setVisibility(View.GONE);
                ll_sendgifts.setVisibility(View.VISIBLE);
                SendGifts1v1Bean sendGifts1v1Bean = new Gson().fromJson(msg.getExtra(), SendGifts1v1Bean.class);
                YImageUtil.show(iv_imageGift, sendGifts1v1Bean.getImg());
                tv_giftsName.setText(sendGifts1v1Bean.getGiftsName() + "  x" + sendGifts1v1Bean.getGiftsNum());

            } else if (YStringUtil.contains(msg.getExtra(), "qbt")) {//消息提示
                ll_sendgifts.setVisibility(View.GONE);
                SendGifts1v1Bean sendGifts1v1Bean = new Gson().fromJson(msg.getExtra().toString(), SendGifts1v1Bean.class);
                if (("@@@@@**".equals(sendGifts1v1Bean.getType()))) {//通知消息
                    Log.d("===消息==", "======自定义消息体ss1====" + msg.getExtra().toString());
                    msgBodyText.setText(sendGifts1v1Bean.getText() + "");
                    tvServiceContent.setTextSize(13f);
                    llServiceTip.setVisibility(View.GONE);
                } else {
                    Log.d("===消息==", "======自定义消息体ss====" + msg.getExtra().toString());
                    msgBodyText.setVisibility(View.GONE);
                    llServiceTip.setVisibility(View.VISIBLE);
                    tvServiceContent.setText(sendGifts1v1Bean.getText() + "");
                    tvServiceContent.setTextSize(11f);
                }
            }else {
                ViewGroup.LayoutParams layoutParams = ll_ItemLayout.getLayoutParams();
                layoutParams.width = (int) ViewGroup.LayoutParams.WRAP_CONTENT;
                layoutParams.height = (int) ViewGroup.LayoutParams.WRAP_CONTENT;
                if (TextUtils.equals("[自定义消息]", msg.getExtra().toString())) {
//                    msgBodyText.setText(Html.fromHtml(TUIKitConstants.covert2HTMLString("[不支持的自定义消息]")));
                } else if (msg.getExtra().toString().contains("action")) {
                    llServiceTip.setVisibility(View.GONE);
                    try {
                        JSONObject jsonObject = new JSONObject(msg.getExtra().toString());
                        String reason = jsonObject.optString("reason");
                        int action = jsonObject.optInt("action");
                        if (action == 100) {
                            msgBodyText.setText("请求与您连麦");
                        } else if (action == 101) {
                            int accept = jsonObject.optInt("accept");
                            if (0 == accept) {
                                msgBodyText.setText("拒绝连麦");
                            } else {
                                msgBodyText.setText("接受连麦");
                            }
                        }else if (action==102){
                            msgBodyText.setText("断开连麦");
                        }else {
                            msgBodyText.setText(reason);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    llServiceTip.setVisibility(View.GONE);
                    msgBodyText.setText(msg.getExtra().toString());
                }

                ll_sendgifts.setVisibility(View.GONE);
            }

        }
        if (properties.getChatContextFontSize() != 0) {
            msgBodyText.setTextSize(properties.getChatContextFontSize());
        }
        if (msg.isSelf()) {
            if (properties.getRightChatContentFontColor() != 0) {
                msgBodyText.setTextColor(properties.getRightChatContentFontColor());
            }
            msgBodyText.setTextColor(Color.parseColor("#ffffff"));
        } else {
            if (properties.getLeftChatContentFontColor() != 0) {
                msgBodyText.setTextColor(properties.getLeftChatContentFontColor());
            }
            msgBodyText.setTextColor(Color.parseColor("#333333"));
        }
    }
}
